#!/usr/bin/env python
#coding=utf-8
# author:wade
# contact: 317909531@qq.com
# datetime:2020/4/4 21:13
from rest_framework import serializers
from myclass import models
"""
文件说明：专门为构建树状结构图生产的序列类，每个类都都继承关系  学校-年级-班级
                                                        学校-组织-学生
"""

#学校树状机构序列化
class Tree_MS_STUDENT_serializers(serializers.ModelSerializer):
    #起别名
    label= serializers.CharField(source='NAME') #名称
    value = serializers.IntegerField(source='ID')  #主键ID
    class Meta:
        model = models.MS_STUDENT
        # fields = '__all__'
        fields = ['label', 'value']
#学校树状机构序列化
class Tree_MS_CLASS_serializers(serializers.ModelSerializer):
    #起别名
    label= serializers.IntegerField(source='CLASS_NUM') #名称
    value = serializers.IntegerField(source='CLASS_NUM')  #主键ID
    # children = Tree_MS_STUDENT_serializers(many=True, read_only=True,source='ms_students')

    class Meta:
        model = models.MS_CLASS
        # fields = '__all__'
        fields = ['label', 'value']

#学校树状机构序列化
class Tree_MS_GRADE_serializers(serializers.ModelSerializer):
    # ms_grades = serializers.StringRelatedField(many=True)  # 一找多，用_str_字段展示关系
    # ms_grades = serializers.PrimaryKeyRelatedField(many=True,read_only=True)  # 一找多，用关系表的主键展示关系
    # ms_grades = serializers.SlugRelatedField(many=True,read_only=True,slug_field='STATUE') # 一找多，用关系表的内容属性展示关系
    children = Tree_MS_CLASS_serializers(many=True, read_only=True,source='ms_classes')
    #起别名
    label= serializers.IntegerField(source='ID') #名称
    value = serializers.IntegerField(source='ID')  #主键ID

    class Meta:
        model = models.MS_GRADE
        # fields = '__all__'
        fields = ['label', 'value','children']
#学校树状机构序列化

'''
文件说明：根据queryset是狮子庙中学，查询返回狮子庙中学子集内容序列化
'''


class Tree_MS_COLLEGE_serializers(serializers.ModelSerializer):
    # ms_grades = serializers.StringRelatedField(many=True)  # 一找多，用_str_字段展示关系
    # ms_grades = serializers.PrimaryKeyRelatedField(many=True,read_only=True)  # 一找多，用关系表的主键展示关系
    # ms_grades = serializers.SlugRelatedField(many=True,read_only=True,slug_field='STATUE') # 一找多，用关系表的内容属性展示关系
    children = Tree_MS_GRADE_serializers(many=True, read_only=True,source='ms_grades')

    #起别名
    label= serializers.CharField(source='COLLEGE_NAME') #名称
    value = serializers.IntegerField(source='ID')  #主键ID
    class Meta:
        model = models.MS_COLLEGE
        # fields = '__all__'
        fields = ['label', 'value','children']